import Vue from "vue";
import App from "./App";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
// import "element-ui/lib/theme-chalk/base.css";
import VueRouter from "vue-router";
import routes from "./routes";

Vue.use(ElementUI);
Vue.use(VueRouter);

// NProgress.configure({ showSpinner: false });

const router = new VueRouter({
    routes
});

router.beforeEach((to, from, next) => {
    // NProgress.start();
    if (to.path == "/login") {
        //如果页面为登录页 清除sessionStorage中数据
        window.sessionStorage.removeItem("data");
        window.sessionStorage.removeItem("userName");
        window.sessionStorage.removeItem("token");
    }

    //重置网页title
    let documentTitle = "管理后台";
    to.matched.forEach(path => {
        if (path.path) {
            documentTitle += ` - ${path.name}`;
        }
    });
    document.title = documentTitle;

    let user = window.sessionStorage.getItem("token");
    if (!user && to.path != "/login") {
        //login登录 不用带token 其他页面如果没有的话 强制去登录页
        next({ path: "/login" });
    } else {
        next();
    }
});
router.afterEach(transition => {
    // NProgress.done();
});

new Vue({
    router,
    render: h => h(App)
}).$mount("#app");

export default router;
